using System;
using System.Collections.Generic;
using System.Text;
using XEngine;
using Microsoft.Xna.Framework;

namespace XEnginePhysicsGame
{
    public class Scene
    {
        XMain X;
        Game1 Parent;

        List<XActor> objects = new List<XActor>();

        public int CurrentBall;
        public XActor Ball { get { return objects[CurrentBall]; } }

        XModel ball_model;

        public Vector3 SpawnPoint;

        public Scene(XMain X, Game1 Parent)
        {
            this.X = X;
            this.Parent = Parent;
        }

        public void Load()
        {
            XResourceGroup resources = new XResourceGroup(X);

            ball_model = new XModel(X, @"Content\Models\Sphere_1");

            resources.AddComponent(ball_model);

            resources.Load();
        }

        XModel Level_Model;
        XActor Level_Actor;

        public void LoadLevel(int Level)
        {
            if (Level_Model != null)
                X.Components.Remove(Level_Model);

            if (Level_Actor != null)
            {
                Level_Actor.Disable();
                X.Components.Remove(Level_Actor);
            }

            Level_Model = new XModel(X, @"Content\Models\Levels\Level" + Level);
            Level_Model.Load(Parent.Content);

            Level_Actor = new XActor(X, XActor.ActorType.TriangleMesh, Level_Model, Vector3.Zero, Matrix.Identity, Vector3.One, Vector3.Zero, new Vector3(10000), Vector3.Zero, 100000000);
            //Level_Actor.PhysicsBody.PhysicsSkin.SetMaterialProperties(0, new JigLibX.Collision.MaterialProperties(.10f, .2f, .1f));
        }

        public enum ObjectType { Ball, OrangeFloorBlock, OrangeFloorBlock3x3, Checkpoint }

        public int AddObject(ObjectType Type, Vector3 Position, Vector3 Rotation)
        {
            Matrix Orientation = Matrix.CreateFromYawPitchRoll(MathHelper.ToRadians(Rotation.Y), MathHelper.ToRadians(Rotation.X), MathHelper.ToRadians(Rotation.Z));

            if (Type == ObjectType.Ball)
            {
                objects.Add(new XActor(X, XActor.ActorType.Sphere, ball_model, Position, Orientation, new Vector3(1), Vector3.Zero, new Vector3(1.9f), Vector3.Zero, 3000));
                objects[objects.Count - 1].PhysicsBody.PhysicsBody.AllowFreezing = false;
            }

            return objects.Count - 1;
        }

        public void Update(GameTime gameTime)
        {
            if (Ball.Position.Y < -100f)
                Ball.Position = SpawnPoint;
        }
    }
}
